const router = require('koa-router')()

router.prefix('/routers')

const checkToken = require("../static/checkToken");



const getList = async (ctx) => {
    let sql = "select * from router"
    let result = await ctx.db.EXCUTE(sql, [])
    let Arr = result.reverse();
    let page = ctx.request.body.page;
    if((page - 1) * 10 >= result.length) {
        let arr = Arr.slice((page - 2) * 10 , (page - 1) * 10);
        return {infoList: arr, length: result.length};
    } else {
        let arr = Arr.slice((page - 1) * 10 , page * 10);
        return {infoList: arr, length: result.length};
    }
}

const produceId = () => {
    return Math.random().toString(36).substr(3) + Date.now().toString(36);
}

router.post('/getData', async(ctx, next) => {
    let checkRes = await checkToken.cheakToken(ctx);
    if(checkRes) {
        let res = await checkToken.cheakToken(ctx);
        let {infoList, length} = await getList(ctx);
        console.log("token检验成功");
        ctx.body = {code: "200", msg: "成功获取数据", infoList, length}
    } else {
        ctx.body = {code: "500", msg: "请先登录"}
    }
});

router.post('/editData', async(ctx, next) => {
    let checkRes = await checkToken.cheakToken(ctx);
    if(checkRes) {
        let data = ctx.request.body;
        let {infoList} = await getList(ctx);
        let sql = "insert into router (router_name, router_way, router_address, router_phone, router_id) values (?,?,?,?,?)"
        let sql1 = "update router set router_name = ?, router_way = ?, router_address = ?, router_phone = ? where router_id = ?";
        let sql2 = "select * from router where router_name = ?"
        let sql3 = "select * from router where router_id = ?"
        // console.log(DataArr.findIndex(item => item.router_id == data.router_id));
        if(infoList.findIndex(item => item.router_id == data.router_id) == -1) {
            let res = await ctx.db.EXCUTE(sql2, [data.router_name]);
            
            let result = await ctx.db.EXCUTE(sql, [data.router_name, data.router_way, data.router_address, data.router_phone, produceId()]);
            if(res.length >= 1) {
                ctx.body = {code: "500", msg: "该线路已存在，请重新输入"}
            } else {
                console.log(result);
                let {infoList, length} = await getList(ctx);
                ctx.body = {code: "200", msg: "添加成功", infoList, length}
            }
        } else {
            let res = await ctx.db.EXCUTE(sql2, [data.router_name]);
            let res1 = await ctx.db.EXCUTE(sql3, [data.router_id]);
            if(res.length == 1 && res1[0].router_name != data.router_name) {
                ctx.body = {code: "500", msg: "该路线已存在，请重新输入"}
            } else if(res.length == 0 || (res.length == 1 && res1[0].router_name == data.router_name)) {
                let result = await ctx.db.EXCUTE(sql1, [data.router_name, data.router_way, data.router_address, data.router_phone, data.router_id]);
                
                console.log(result);
                let {infoList, length} = await getList(ctx);
                ctx.body = {code: "200", msg: "修改成功", infoList, length}
            }
    
        }
    } else {
        ctx.body = {code: "500", msg: "请先登录"}
    }

    
});

router.post("/deleteData", async (ctx, next) => {
    let checkRes = await checkToken.cheakToken(ctx);
    if(checkRes) {
        let sql = "delete from router where router_id = ?"
        let result = await ctx.db.EXCUTE(sql, [ctx.request.body.router_id]);
        let {infoList, length} = await getList(ctx);
    
        ctx.body = {code: "200", msg: "成功删除", infoList, length};
    } else {
        ctx.body = {code: "500", msg: "请先登录"}
    }
})

router.post("/searchData", async (ctx, next) => {
    let checkRes = await checkToken.cheakToken(ctx);
    if(checkRes) {
        let sql = 'select * from router where router_name like ?';
        let mz = `%${ctx.request.body.searchContent}%`
        let result = await ctx.db.EXCUTE(sql, [mz]);
        let Arr = result.reverse();
        let arr = Arr.slice((ctx.request.body.page - 1) * 10 , ctx.request.body.page * 10);
    
        ctx.body = {code: '200', msg: "成功搜索", infoList: arr, length: result.length}
    } else {
        ctx.body = {code: "500", msg: "请先登录"}
    }
});

router.post("/handleTop", async (ctx, next) => {
    let checkRes = await checkToken.cheakToken(ctx);
    if(checkRes) {
        let data = ctx.request.body;
        let sql = "select * from router where router_id = ?";
        let sql1 = "insert into router (router_name, router_way, router_address, router_phone, router_id) values (?,?,?,?,?)";
        let sql2 = "delete from router where router_id = ?"
        let result1 = await ctx.db.EXCUTE(sql, [data.router_id]);
        let result2 = await ctx.db.EXCUTE(sql2, [data.router_id]);
        let result3 = await ctx.db.EXCUTE(sql1, [result1[0].router_name, result1[0].router_way, result1[0].router_address, result1[0].router_phone, result1[0].router_id,]);
        let {infoList} = await getList(ctx);
        ctx.body = {code: "200", msg: "置顶成功", infoList}
    } else {
        ctx.body = {code: "500", msg: "请先登录"}
    }
});

module.exports = router